package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	
	public IndexServlet() {
		System.out.println("IndexServlet() ....");
	}
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = null;
		System.out.println("IndexServlet  doGet() ....");
		/*
		Cookie[] cookies = request.getCookies();
		if(cookies != null) {
			for(Cookie cookie : cookies) {
				System.out.println(cookie.getName() + ":" + cookie.getValue());
				if("username".equals(cookie.getName())) {
					username = cookie.getValue();
				}
			}
		}
		*/
		
		HttpSession session = request.getSession();
		username = (String) session.getAttribute("username");
		
		request.setAttribute("username",username);
		
		request.getRequestDispatcher("index.jsp").forward(request, response);
		
		//request.removeAttribute(name);
		//session.removeAttribute(name);
		//request.getServletContext().removeAttribute(name);
		
		//Request 请求
		//Session  会话  对应一个用户
		//ServletContext 应用/容器上下文  跨用户
		
		//生命周期  ServletContext》Session》Request
		//传值选择 Request-->Session-->ServletContext
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
